package com.lxw;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;

public class Consumer05_header_email {
    //队列名
    //private static final String QUEUE_HEADER_EMAIL = "queue_header_email";
    private static final String EXCHANGE_HEADER_INFORM = "exchange_header_inform";

    public static void main(String[] args)throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        //创建一个连接
        Connection connection = connectionFactory.newConnection();
        //创建与交换机的通道，每个通道代表一个会话
        Channel channel = connection.createChannel();

        HashMap<String, Object> header_email = new HashMap<>();
        header_email.put("inform_type","sms");
        //header_email.put("inform_type2","email");
        /**
         * exchangeDeclare(交换机名称，交换机类型)
         * 交换机的类型有：fanout、topic、direct、headers
         */
        channel.exchangeDeclare(EXCHANGE_HEADER_INFORM, BuiltinExchangeType.HEADERS);
//        channel.queueDeclare(QUEUE_HEADER_EMAIL,true,false,false,null);
  //      channel.queueBind(QUEUE_HEADER_EMAIL,EXCHANGE_HEADER_INFORM,"",header_email);
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue,EXCHANGE_HEADER_INFORM,"",header_email);


        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body,"utf-8");
                System.out.println(message);
            }
        };
        channel.basicConsume(queue,true,defaultConsumer);
        System.out.println(queue);
    }
}
